<?php


namespace App\Service;


use Ramsey\Uuid\Uuid;

/**
 * 文件操作基础类
 * Class FileBase
 * @package App\Service
 */
class FileBase {

    protected $fileId;

    public function __construct() {
        $this->fileId = Uuid::uuid1()->toString();
    }

    /**
     * 获取文件id
     * @return string
     */
    public function getFileId() {
        return $this->fileId;
    }

    /**
     * 根据文件名获取hash打散之后的存储路径
     * @param $module
     * @return string
     */
    public function generateHashStorePath() {
        $hashCode = $this->hashCode($this->fileId);
        $dir1 = $hashCode & 0xf;
        $dir2 = ($hashCode >> 4) & 0xf;
        $dir3 = ($hashCode >> 8) & 0xf;
        $hashDir = '/' . $dir1 . '/' . $dir2 . '/' . $dir3;

        return $hashDir;
    }

    /**
     * 获取字符串的hashcode
     * @param $str
     * @return int
     */
    protected function hashCode($str) {
        $str = strtoupper($str);
        $mdv = md5($str);
        $mdv1 = substr($mdv, 0, 16);
        $mdv2 = substr($mdv, 16, 16);
        $crc1 = abs(crc32($mdv1));
        $crc2 = abs(crc32($mdv2));
        return \bcmul($crc1, $crc2);
    }
}